<template>
  <div class="app-pro-basicLayout-content-wrap">
    <div class="app-pro-pages-content">
      <div class="app-pro-pages-table" style="min-height:calc(100vh - 100px)">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="ruleForm-password">
          <!-- <el-form-item label="비밀번호" prop="password">
            <el-input v-model="ruleForm.password" type="password" placeholder="비밀번호를 입력해주세요."></el-input>
          </el-form-item> -->
          <el-form-item label="새 비밀번호" prop="newPassword">
            <el-input v-model="ruleForm.newPassword" type="password" placeholder="새로운 비밀번호를 입력해주세요."></el-input>
          </el-form-item>
          <el-form-item label="비밀번호 확인" prop="password_confirmation">
            <el-input v-model="ruleForm.password_confirmation" type="password" placeholder="새로운 비밀번호 확인을 입력해주세요."></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">확인</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { restPassword } from '@/api/app'
export default {
  name: 'password',
  data () {
    var validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('새로운 비밀번호를 입력해주세요.'))
      } else if (value.toString().length < 6 || value.toString().length > 18) {
        callback(new Error(' 6 - 18 자리수 비밀번호를 입력해주세요.'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('비밀번호를 입력해주세요.'))
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('비밀번호와 비밀번호 확인이 다릅니다.'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        // password: '',
        newPassword: '',
        password_confirmation: ''
      },
      rules: {
        // password: [
        //   { required: true, message: '비밀번호를 입력해주세요.', trigger: 'blur' }
        // ],
        newPassword: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        password_confirmation: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          restPassword({ password: this.ruleForm.newPassword, password_confirmation: this.ruleForm.password_confirmation }).then(res => {
            this.$message({
              type: 'success',
              message: '비밀번호 변경 성공'
              // message: '비밀번호 변경이 정상적으로 완료되었습니다.'
            })
          }).finally(() => {
            this.ruleForm.password_confirmation = this.ruleForm.newPassword = this.ruleForm.password = ''
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style>
.ruleForm-password {
  width: 500px;
  padding-top: 50px;
  margin: 0 auto;
}
</style>
